import { Toast } from "vant";

export const myMixins = {
  data () {
    return {
		appid:'',
		secret:''
      // sharetitle: '活动宝',
      // share_describe: '商户引流，增加客源，助力产品推广',
      // shareimg: 'https://www.umiyun.net/addons/youmi_activitys/public/uploads/c1bab710a6db68e7f308653929d9c460.jpg'
    }
  },
  methods: {
	getWeixinConfig() {
	  const _this = this;
	  let params = {}
	  _this.$request.getWeixinConfig(params).then(res=>{
	    this.appid = res.data.value.appid
	    this.secret = res.data.value.secret
	    //_this.getCode(res.data.value.appid)
	    if (process.env.NODE_ENV == 'production') {
	      _this.getCode(res.data.value.appid)
	    }
	  })
	},
	getCode (appid) { // 非静默授权，第一次有弹框
	  const _this = this;
	  const code = this.getUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
	  const local = window.location.href
	  if (code == null || code === '') {
	    window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+ appid +'&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
	  } else {
	    _this.$store.commit('setCode', code)
	    _this.getOpenId(code) //把code传给后台获取用户信息
	  }
	},
	getUrlParam(name) {
	  var reg=new RegExp('(^|&)'+name+'=([^&]*)(&|$)')
	  var r=window.location.search.substr(1).match(reg)
	  if(r!=null) return unescape(r[2])
	  return null
	},
	getOpenId(code) {
	  const _this = this;
	  let params = {
	      appid: _this.appid,
	      secret: _this.secret,
	      code: code
	  }
	  _this.$request.getOpenid(params).then(res=>{
	      const userInfo = res.data[0];
	      const openid = userInfo["openid"];
	      localStorage.setItem('OPENID', openid)
	  })
	},
    toView(path){
    	this.$router.push(path)
    },
    configItemShare() {
      const _this = this;
      let params = {}
      _this.$request.configItemShare(params).then(res=>{
        if(res.ret == 200){
          _this.sharetitle = res.data.value.sharetitle
          _this.share_describe = res.data.value.share_describe
          _this.shareimg = res.data.value.shareimg
          _this.shareInit()
        }else{
          Toast('configJssdk error')
        }
      })
    },
    shareInit() {
      const _this = this;
      const url = (location.href.split('#')[0])
      let params = {
          url: url
      }
      _this.$request.configJssdk(params).then(res=>{
        if(res.ret == 200) {
          wx.config({
            debug: false,// 是否开启调试模式
            appId: res.data.appId,//appid
            timestamp: res.data.timestamp,// 时间戳
            nonceStr: res.data.nonceStr,// 随机字符串
            signature: res.data.signature,// 签名
            jsApiList: ['checkJsApi', 'updateAppMessageShareData', 'updateTimelineShareData']// 需要使用的JS接口列表
          })
          const dataShare = { 
            title: _this.sharetitle,
            desc: _this.share_describe, // 分享描述
            link: "https://www.umiyun.net/addons/youmi_activitys/public/mobile/index.html", // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: _this.shareimg, // 分享图标
            success: function () {
                // 设置成功
            },
            fail:function(error){
              alert(JSON.stringify(error))
            }
          }
          wx.updateAppMessageShareData(dataShare)
          wx.updateTimelineShareData(dataShare)
          wx.error(function(error) {
              alert(JSON.stringify(error))
          })
        }else{
          Toast('configJssdk error')
        }
      })
    }
  }
}